package com.eco.fanliapp.utils;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class WeiBoDwzUtils {

    public static String createShortUrl(String longUrl) {

        String urlEncoder = null;
        String shortUrl = null;
        try {
            urlEncoder = URLEncoder.encode(longUrl, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        longUrl = "http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=" + urlEncoder;
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
        String shortUrls = HttpUtils.sendGet(longUrl, "");

        shortUrl = subCenter(shortUrls, "\"url_short\":\"", "\"");
        shortUrl = shortUrl.replaceFirst("http","https");

        return shortUrl;
    }
    public static String subCenter(String str, String a, String b) {
        try {
            String c = str.substring(str.indexOf(a) + a.length());
            return c.substring(0, c.indexOf(b));
        } catch (Exception e) {
            return "";
        }
    }
}
